package su.myspringapps.onlineschool.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import su.myspringapps.onlineschool.entity.Course;
import su.myspringapps.onlineschool.entity.Student;
import su.myspringapps.onlineschool.entity.Subscription;
import su.myspringapps.onlineschool.entity.Teacher;

import java.io.IOException;
import java.util.Objects;
import java.util.Properties;

/**
 * Класс MainConfiguration
 * <p/>
 * Главный класс конфигурации приложения
 * <p/>
 *
 * @author petr.mikhailin
 * created 12.05.2024 17:59
 */
@Configuration
public class MainConfiguration {

    private final ApplicationContext applicationContext;

    @Autowired
    public MainConfiguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public SessionFactory sessionFactory() throws IOException {
        org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
        Properties properties = new Properties();
        properties.load(Objects.requireNonNull(
                applicationContext.getClassLoader()).getResourceAsStream("application.properties"));
        configuration.setProperties(properties);
        configuration.addAnnotatedClass(Student.class);
        configuration.addAnnotatedClass(Teacher.class);
        configuration.addAnnotatedClass(Course.class);
        configuration.addAnnotatedClass(Subscription.class);
        return configuration.buildSessionFactory();
    }

}
